package com.zh.ssmshirofreemarker.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.util.Properties;

@Configuration
public class SpringMVCConfig {


    /**
     * 配置出现不同的异常时，跳转到不同的页面
     * @return
     */
    @Bean
    public SimpleMappingExceptionResolver simpleMappingExceptionResolver(){

        SimpleMappingExceptionResolver simpleMappingExceptionResolver
                = new SimpleMappingExceptionResolver();
        Properties props = new Properties();
        props.setProperty("org.apache.shiro.authz.UnauthorizedException","error/403");
        props.setProperty("org.apache.shiro.authz.UnauthenticatedException","/login");
        props.setProperty("java.lang.NumberFormatException","error");
        simpleMappingExceptionResolver.setExceptionMappings(props);
        return simpleMappingExceptionResolver;
    }

    @Bean("freemakerCongfig")
    public ShiroTagFreeMarkerConfigurer freeMarkerConfig(){
        ShiroTagFreeMarkerConfigurer shiroTagFreeMarkerConfigurer = new ShiroTagFreeMarkerConfigurer();
        shiroTagFreeMarkerConfigurer.setTemplateLoaderPath("classpath:/templates");

        Properties props = new Properties();
        props.setProperty("defaultEncoding","UTF-8");
        props.setProperty("classic_compatible","true");
        shiroTagFreeMarkerConfigurer.setFreemarkerSettings(props);
        return shiroTagFreeMarkerConfigurer;
    }


}
